﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evolution
{
    /// <summary>
    /// This class 
    /// </summary>
    class ConsoleEngine:Engine
    {
        public ConsoleEngine(Int16 Size)
        {                   
            this.Size=(byte)(Size+2);
            this.Matrix = new byte[Size + 2, Size + 2];            
        }

        /// <summary>
        /// Print matrix:
        ///     ' ' - empty element
        ///     'o' - 1st type
        ///     'x' - 2nd type
        /// </summary>
        public override void PrintMatrix()
        {
            for (Int16 i = 1; i < Size - 1; i++)
            {
                for (Int16 j = 1; j < Size - 1; j++)
                    switch (Matrix[i, j])
                    {
                        case 0x0000: { Console.Write(' '); break; }
                        case 0x0001: { Console.Write('o'); break; }
                        case 0x0010: { Console.Write('x'); break; }
                    }
                Console.Write("\n");
            }
        }

        /// <summary>
        /// Download, read and save data.
        /// </summary>
        public override void Setting()
        {
            //base.Setting();
            char Key = '\0';
            Console.WriteLine("_ - nothing\no - 1st type\nx - 2nd type\nAttention! All input numerals are integer: 0..100\n");
            if (File.Exists("setting.ini"))
            {
                EnterFile();
                Console.WriteLine("Existing file \"setting.ini\".\n Current options are follow:");
                Console.WriteLine("  First  type:"); Type1.PrintValues(Percent1);
                Console.WriteLine("  Second type:"); Type1.PrintValues(Percent2);
                Console.Write("Do you want to change setting?(y/n)");
                while ((Key != 'y') && (Key != 'Y') && (Key != 'n') && (Key != 'N'))
                    Key = Console.ReadKey().KeyChar;
                if ((Key == 'y') || (Key == 'Y'))
                {
                    Console.WriteLine("");
                    EnterConsole();
                    SaveToSile();
                }
            }
            else
            {
                Console.WriteLine("File \"setting.ini\" does not exist!");
                EnterConsole();
                SaveToSile();
            }
            Console.Clear();
            SetRandom();
            PrintMatrix();
        }



        private void SaveToSile()
        {
            File.Delete("setting.ini");

            StreamWriter OutputFile=new StreamWriter(@"setting.ini",true);
            OutputFile.WriteLine("[Global Setting]");
            OutputFile.WriteLine("\tPercent1 = " + Convert.ToString(Percent1));
            OutputFile.WriteLine("\tPercent2 = " + Convert.ToString(Percent2));
            OutputFile.WriteLine("[Type1]");
            Type1.Save(ref OutputFile);
            OutputFile.WriteLine("[Type2]");
            Type2.Save(ref OutputFile);
            OutputFile.WriteLine("[End]");
            OutputFile.Close();
        }

        /// <summary>
        /// Read data from file.
        /// </summary>
        private void EnterFile()
        {
            StreamReader InputFile = new StreamReader(@"setting.ini");
            string Temp = InputFile.ReadLine();
            while (Temp[0]!='[') Temp = InputFile.ReadLine();
            while (Temp != "[End]") 
            {
                switch (Temp)
                {
                    case "[Global Setting]":
                    { 
                        byte[] Result={0,0};
                        string[] Strings={"Percent1","Percent2"};
                        Temp = IniReader.ReadSection(InputFile, ref Result, Strings);
                        Percent1 = Result[0];
                        Percent2 = Result[1];
                        break;
                    }
                    case "[Type1]":
                    {
                        byte[] Result = { 0, 0, 0 };
                        string[] Strings = { "MinValueToLive", "MaxValueToLive", "MinValueToBorn" };
                        Temp = IniReader.ReadSection(InputFile, ref Result, Strings);
                        Type1 = new TypeInfo(Result[0], Result[1], Result[2]);
                        break;
                    }
                    case "[Type2]":
                    {
                        byte[] Result = { 0, 0, 0 };
                        string[] Strings = { "MinValueToLive", "MaxValueToLive", "MinValueToBorn" };
                        Temp = IniReader.ReadSection(InputFile, ref Result, Strings);
                        Type2 = new TypeInfo(Result[0], Result[1], Result[2]);
                        break;
                    }
                    default: 
                    { 
                        byte[] Result = { };
                        string[] Strings = { };
                        Temp = IniReader.ReadSection(InputFile, ref Result, Strings);
                        break;
                    }
                }
            }
            InputFile.Close();
        }

        /// <summary>
        /// Read data from user.
        /// </summary>
        private void EnterConsole()
        {
            Console.Write("Enter percents #1: ");
            Byte.TryParse(Console.ReadLine(), out Percent1);
            Console.Write("Enter percents #2: ");
            Byte.TryParse(Console.ReadLine(), out Percent2);
            Type1 = new TypeInfo("type #1");
            Type2 = new TypeInfo("type #2");
        }

    }
}
